package com.allenkerr.rpc_sample.service.impl;

import com.allenkerr.rpc.server.RpcService;
import com.allenkerr.rpc_sample.Service.UserService;
import com.allenkerr.rpc_sample.pojo.User;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created by 小柯(Allen Kerr) on 17.5.24 10:46.
 */
@RpcService(UserService.class)
public class UserServiceImpl implements UserService {

    /**
     * 为简化该项目，就不建数据库和DAO了，users集合由spring注入
     * ——> 不知道为什么注入之后无法获取，待查
     */
    private static final List<User> USERS = new ArrayList<>();

    static {
        USERS.add(new User(1, "Tom", "123"));
        USERS.add(new User(2, "Neal", "123"));
        USERS.add(new User(3, "Jack", "123"));
        USERS.add(new User(4, "Rose", "123"));
        USERS.add(new User(5, "Tim", "123"));
        USERS.add(new User(6, "Mike", "123"));
    }

    /**
     * 根据 userId 查询 user
     *
     * @param userId
     * @return
     */
    @Override
    public User getUserById(Integer userId) {
        return USERS.stream()
                    .filter(Objects::nonNull)
                    .filter(user -> user.getUserId() != null)
                    .filter(user -> user.getUserId().equals(userId))
                    .findFirst()
                    .orElse(null);
    }

    /**
     * 查询所有用户信息
     *
     * @return
     */
    @Override
    public List<User> getAll() {
        return USERS;
    }

}
